package com.ssm.zhihu_ssm.controller;


import com.ssm.zhihu_ssm.entity.Letter;
import com.ssm.zhihu_ssm.entity.LetterReturn;
import com.ssm.zhihu_ssm.entity.User;
import com.ssm.zhihu_ssm.service.LetterService;
import com.ssm.zhihu_ssm.service.UserService;
import com.ssm.zhihu_ssm.util.ReturnMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class LetterController {

    @Autowired
    private UserService userService;
    @Autowired
    private LetterService letterService;

    @RequestMapping("/findNearLetter")//得到用户给他发的私信
    @ResponseBody
    private Map<String, Object> findNearLetter(String phone) {
        int x = userService.findId(phone);

        List<LetterReturn>  letters= letterService.findLetterReturn(x);

        System.out.println(letters);
        Map<String, Object> map = ReturnMap.ReturnMapS(letters);
        System.out.println(map);
        return map;
    }

    @RequestMapping("/findLetterWith")//得到与某个用户发的私信
    @ResponseBody
    private Map<String, Object> findAllLetter(String phone,int fromID) {
        int x = userService.findId(phone);
        List<Letter>  letters= letterService.findLetterWith(x,fromID);
        System.out.println(letters);
        Map<String, Object> map = ReturnMap.ReturnMapS(letters);
        System.out.println(map);
        return map;
    }


    @RequestMapping("/delectLetter")//删除私信
    @ResponseBody
    private Map<String, Object> delectLetter(int id) {
        boolean letters = letterService.delectLetter(id);
        Map<String, Object> map = ReturnMap.ReturnMapS(letters);
        System.out.println(map);
        return map;
    }


    @RequestMapping("/addLetter")//发送私信
    @ResponseBody
    private Map<String, Object> addLetter(String phone ,int fromid,String content) {
        int x = userService.findId(phone);
        boolean letters = letterService.addletters(x,fromid,content);
        Map<String, Object> map = ReturnMap.ReturnMapS(letters);
        System.out.println(map);
        return map;
    }

}
